package gob.osinerg.emergencia.action;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.apache.struts.action.ActionMessages;
import org.apache.struts.actions.DispatchAction;
import org.displaytag.tags.TableTagParameters;
import org.displaytag.util.ParamEncoder;

public class GenericAction extends DispatchAction{

	public static String PRUEBAS = "PRUEBAS";
	public String modo = PRUEBAS;
	public Long beanIDD = new Long(9999);
	
	private Log log ;
	protected String encodedValue="";
	protected String pageNumber="";
	
	public GenericAction() {
		log =  LogFactory.getLog(GenericAction.class);
	}
	
	
	public void saveSuccessfulMessage(HttpServletRequest request,String key){
        ActionMessages messages = new ActionMessages();
        messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key));
        saveMessages(request, messages);
    }
   
    public void sendSuccessfulMessage(HttpServletRequest request,String key){
        ActionMessages messages = new ActionMessages();
        messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key));
        saveMessages(request, messages);
    }
    public void sendSuccessfulMessage(HttpServletRequest request,String key, String param){
        ActionMessages messages = new ActionMessages();
        messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key,param));
        saveMessages(request, messages);
    }    
    
    public void sendSuccessfulMessage(HttpServletRequest request,String key, Object[] params){
        ActionMessages messages = new ActionMessages();
        messages.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key, params));
        saveMessages(request, messages);
    }
   
    public void sendErrorMessage(HttpServletRequest request,String key){
        ActionErrors errors = new ActionErrors();
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key));
        saveErrors(request, errors);
    }

    public void sendErrorMessage(HttpServletRequest request,String key, String param){
        ActionErrors errors = new ActionErrors();
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key, param));
        saveErrors(request, errors);
    }
    
    public void sendErrorMessage(HttpServletRequest request,String key, Object[] params){
        ActionErrors errors = new ActionErrors();
        errors.add(ActionMessages.GLOBAL_MESSAGE, new ActionMessage(key, params));
        saveErrors(request, errors);
    }
    
    public ActionForward doNavigate(ActionMapping mapping,
            ActionForm actionForm,
            HttpServletRequest request,
            HttpServletResponse response)
            throws Exception {
        log.debug("************************************************************");
        String forwardName = request.getParameter("forwardName");
        log.debug("...forwardName:"+ forwardName);
        return mapping.findForward(forwardName);
        
    }
    
    public ActionForward execute(ActionMapping actionMapping, ActionForm arg1, HttpServletRequest request, HttpServletResponse arg3) throws Exception {

        encodedValue = (new ParamEncoder("detalle")).encodeParameterName(TableTagParameters.PARAMETER_PAGE);
        if(request.getParameter(encodedValue)!=null)
            pageNumber = request.getParameter(encodedValue);
            if (!request.getParameter("method").equals("cargar") && !request.getParameter("method").equals("changePage") )
                pageNumber = "1";
            log.debug(" ENCODE : "+encodedValue);
            log.debug(" PAGINA : "+pageNumber);
    	return super.execute(actionMapping, arg1, request, arg3);
    }
    
    
    public ActionForward changePage(ActionMapping actionMapping, ActionForm  actionForm,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {
            log.debug(" INI ACTION : changePage");
            log.debug(" FIN ACTION : changePage");
            return actionMapping.findForward("inicio");
    }  
    
}
